/*
 *  Copyright (c) 2017 Tran Le Duy
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.duy.pascal.interperter.libraries.android.view.dialog;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.util.AndroidRuntimeException;
import android.widget.SeekBar;

import com.duy.pascal.ui.utils.DLog;
import com.googlecode.sl4a.facade.AndroidEvent;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Wrapper class for dialog box with seek bar.
 *
 * @author MeanEYE.rcf (meaneye.rcf@gmail.com)
 */
public class SeekBarDialogTask extends DialogTask {

    private final int mProgress;
    private final int mMax;
    private final CharSequence mTitle;
    private final CharSequence mMessage;
    private SeekBar mSeekBar;
    private CharSequence mPositiveButtonText;
    private CharSequence mNegativeButtonText;

    public SeekBarDialogTask(int progress, int max, CharSequence title, CharSequence message) {
        mProgress = progress;
        mMax = max;
        mTitle = title;
        mMessage = message;
    }

    public void setPositiveButtonText(CharSequence text) {
        mPositiveButtonText = text;
    }

    public void setNegativeButtonText(CharSequence text) {
        mNegativeButtonText = text;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mSeekBar = new SeekBar(getActivity());
        mSeekBar.setMax(mMax);
        mSeekBar.setProgress(mProgress);
        mSeekBar.setPadding(10, 0, 10, 3);
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onStartTrackingTouch(SeekBar arg0) {
            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                AndroidEvent eventFacade = getEventFacade();
                if (eventFacade != null) {
                    JSONObject result = new JSONObject();
                    try {
                        result.put("which", "seekbar");
                        result.put("progress", mSeekBar.getProgress());
                        result.put("fromuser", fromUser);
                        eventFacade.postEvent("dialog", result);
                    } catch (JSONException e) {
                       DLog.e(e);
                    }
                }
            }
        });

        Builder builder = new Builder(getActivity());
        if (mTitle != null) {
            builder.setTitle(mTitle);
        }
        if (mMessage != null) {
            builder.setMessage(mMessage);
        }
        builder.setView(mSeekBar);
        configureButtons(builder, getActivity());
        addOnCancelListener(builder, getActivity());
        mDialog = builder.show();
        mShowLatch.countDown();
    }

    private Builder addOnCancelListener(final Builder builder, final Activity activity) {
        return builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                JSONObject result = new JSONObject();
                try {
                    result.put("canceled", true);
                    result.put("progress", mSeekBar.getProgress());
                } catch (JSONException e) {
                   DLog.e(e);
                }
                dismissDialog();
                setResult(result);
            }
        });
    }

    private void configureButtons(final Builder builder, final Activity activity) {
        DialogInterface.OnClickListener buttonListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                JSONObject result = new JSONObject();
                switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        try {
                            result.put("which", "positive");
                            result.put("progress", mSeekBar.getProgress());
                        } catch (JSONException e) {
                            throw new AndroidRuntimeException(e);
                        }
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        try {
                            result.put("which", "negative");
                            result.put("progress", mSeekBar.getProgress());
                        } catch (JSONException e) {
                            throw new AndroidRuntimeException(e);
                        }
                        break;
                }
                dismissDialog();
                setResult(result);
            }
        };
        if (mNegativeButtonText != null) {
            builder.setNegativeButton(mNegativeButtonText, buttonListener);
        }
        if (mPositiveButtonText != null) {
            builder.setPositiveButton(mPositiveButtonText, buttonListener);
        }
    }
}
